package com.example.poc.food;

import org.springframework.beans.BeanUtils;
import org.springframework.hateoas.IanaLinkRelations;
import org.springframework.hateoas.server.mvc.RepresentationModelAssemblerSupport;
import org.springframework.stereotype.Component;

import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;

@Component
public class FoodModelAssembler extends RepresentationModelAssemblerSupport<Food, FoodModel> {

  public FoodModelAssembler() {
    super(FoodController.class, FoodModel.class);
  }

  @Override
  public FoodModel toModel(Food entity) {
    FoodModel model = new FoodModel();
    BeanUtils.copyProperties(entity, model);
    model.add(linkTo(methodOn(FoodController.class).fetchFoodById(entity.getId())).withSelfRel());
    model.add(linkTo(methodOn(FoodController.class).fetchFoodsAsList()).withRel(IanaLinkRelations.COLLECTION));
    return model;
  }

}
